home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / reparameterizeNurbsSurfaceAl < prev    next >
Encoding:
Text File  |  2003-07-17  |  7.0 KB  |  281 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17.  
  18. //
  19. //    Description :
  20. //        A script to reparameterize the knot sequence along U of a NURBS Surface to 
  21. //        the specified range [a,b]. 
  22. //    LIMITATIONS :
  23. //        1. The NURBS surface should have NO history.
  24. //        2. The "reparametrize" keeps no history.
  25. //        3. a < b.
  26. //  How to use:  select a nurbs Surface (eg. surface1) and enter these commands to
  27. //  reparameterize the surface from 0 to 10:
  28. //
  29. //      reparameterizeNurbsSurfaceAlongU -10 10;  // performs the reparameterization
  30. //      getAttr surface1.minValueU;        // will return -10
  31. //      getAttr surface1.maxValueU;        // will return 10
  32. //      nurbsSurfaceKnotsAlongU;        // will print knot vector of selected surface along U 
  33. //
  34. //
  35.  
  36.  
  37. proc float[] rescale( 
  38.     float $knots[], 
  39.     float $startParm, 
  40.     float $endParm ) 
  41. //
  42. //    Description :
  43. //        To rescale the knots to lie inbetween [0,1]
  44. //
  45. {
  46.     float $scaledKnots[] ;
  47.     
  48.     int $n = size($knots) ;
  49.     if( $n == 0 ) return $scaledKnots ;
  50.  
  51.     float $minKnot = $knots[0] ;
  52.     float $maxKnot = $knots[0] ;
  53.     int $i ;
  54.  
  55.     // get min, max knot.
  56.     //
  57.     for( $i = 1 ; $i < $n ; $i++ ) {
  58.         $minKnot = `min $minKnot $knots[$i]` ;
  59.         $maxKnot = `max $minKnot $knots[$i]` ;    
  60.     }
  61.     float $newMin = $startParm ;
  62.     float $newMax = $endParm ;
  63.     float $diff = $maxKnot - $minKnot ;
  64.     float $scaleFactor = $endParm - $startParm ;
  65.     float $scale = $scaleFactor / $diff ;
  66.     for( $i = 0 ; $i < $n ; $i++ ) {
  67.         float $v = ( $knots[$i] - $minKnot ) ; 
  68.         $scaledKnots[$i] = $newMin + ($v * $scale) ; 
  69.     }
  70.     return $scaledKnots ;
  71. }
  72.  
  73. proc string buildTemporarySurface( 
  74.     float $knotsU[],
  75.     int $degu,
  76.     int $nsu,
  77.     int $fu,
  78.     float $knotsV[],
  79.     int $degv,
  80.     int $nsv,
  81.     int $fv)
  82. //
  83. //    Description :
  84. //        To build a temporary surface
  85. //
  86. {
  87.  
  88.     int $nu = size($knotsU) ;
  89.     if( $nu == 0 || $nsu == 0  ) return " " ;
  90.     int $nv = size($knotsV) ;
  91.     if( $nv == 0 || $nsv == 0  ) return " " ;
  92.  
  93.     int $i, $j ;
  94.  
  95.     // append degree.
  96.     //
  97.     string $args ;
  98.     $args = "surface " ;
  99.     $args = $args + " -du " + $degu ;
  100.     $args = $args + " -dv " + $degv ;
  101.  
  102.     // knot sequence along U.
  103.     //
  104.     for( $i = 0 ; $i < $nu ; $i++ ) {
  105.         $args = $args + " -ku " + $knotsU[$i] ;
  106.     }
  107.  
  108.     // knot sequence along V.
  109.     //
  110.     for( $i = 0 ; $i < $nv ; $i++ ) {
  111.         $args = $args + " -kv " + $knotsV[$i] ;
  112.     }
  113.  
  114.     // form u.
  115.     //
  116.     string $formU ;
  117.     if( $fu == 0 ) $formU = " open " ;    
  118.     else if( $fu == 1 ) $formU = " closed " ;
  119.     else $formU = " periodic " ;
  120.     $args = $args + " -fu " + $formU ;
  121.  
  122.     // form v.
  123.     //
  124.     string $formV ;
  125.     if( $fv == 0 ) $formV = " open " ;    
  126.     else if( $fv == 1 ) $formV = " closed " ;
  127.     else $formV = " periodic " ;
  128.     $args = $args + " -fv " + $formV ;
  129.     
  130.     // control points
  131.     //
  132.     int $ncvu = $degu + $nsu ;    
  133.     int $ncvv = $degv + $nsv ;    
  134.  
  135.     for( $i = 0 ; $i < $ncvu ; $i++ ) {
  136.         for( $j = 0 ; $j < $ncvv ; $j++ ) {
  137.             float $v = 0.0 ;
  138.             $args = $args +  " -p " + $v ;
  139.             $args = $args +  " " + $v ;
  140.             $args = $args +  " " + $v ;
  141.         } // for $j
  142.     } // for $i.
  143.  
  144.     string $srfName = eval($args) ;
  145.     return $srfName ;
  146. }
  147.  
  148. proc int rebuildSurfaceToMatchKnots( string $srf, string $matchSrf, int $dir )
  149. //
  150. //    Description :
  151. //
  152. {
  153.     int $ok = 1 ; 
  154.     string $nodes[] ;
  155.     if( catch( $nodes = `rebuildSurface -ch false -rpo true -fr false -dir $dir -rt 2 $srf $matchSrf` ) ) {
  156.         string $dirStr = " U and V " ;
  157.         if ($dir == 0 ) $dirStr = " U " ;
  158.         if( $dir == 1 ) $dirStr = " V " ;
  159.         string $wstr = "rebuild to match knots in " ;
  160.         $wstr = $wstr + $dirStr ;
  161.         $wstr = $wstr + " failed. " ;
  162.         warning $wstr ;
  163.         $ok = 0 ;
  164.     }    
  165.     return $ok ;
  166. }
  167.  
  168. global proc int reparameterizeNurbsSurfaceAlongU(
  169.     float $startParm,
  170.     float $endParm ) 
  171. //
  172. //    Description :
  173. //        To reparametrize the knot sequence of the nurbs surface 
  174. //        along U to be in the range [$startParm, $endParam].
  175. //    NOTE : works on a NURBS surface of the selection list.
  176. //
  177. {
  178.  
  179.     // valid parameters ?
  180.     //
  181.     if( $startParm >= $endParm ) {
  182.         error "start Parameter should be less than end Parameter" ;
  183.         return 0 ;
  184.     }
  185.  
  186.  
  187.     // 0. Grab the select list.
  188.     //
  189.     string $selList[] ;
  190.     $selList = `ls -sl` ;
  191.  
  192.     // 1. Run filter to select only the NURBS curves.
  193.     //
  194.     global int $gSelectNurbsSurfacesBit ;
  195.     string $srfList[] ;
  196.     $srfList = `filterExpand -ex true -sm $gSelectNurbsSurfacesBit $selList` ;    
  197.     if( size($srfList) == 0 ) {
  198.         warning "No NURBS surface selected" ;
  199.         return 1;
  200.     }
  201.  
  202.     // 2. Work on the last item if more than one NURBS surface in list.
  203.     //
  204.     int $len = size($srfList) ;
  205.     string $lastSrf = $srfList[$len-1] ;
  206.     if( $len != 1 ) {
  207.         string $w = " reparameterizing Knots along U for the last NURBS surface: " + $lastSrf ;
  208.         warning $w ;
  209.     }
  210.  
  211.     // 2.0 Check if surface has history.
  212.     //
  213.     string $hist[] = `listHistory -gl true -pdo true -lf true -f false $lastSrf` ;
  214.     if( size($hist) > 0 ) {
  215.         error "surface has history, reparameterize is pointless" ;
  216.         return 1 ;     
  217.     }        
  218.  
  219.     // 2.1 Get the degree.
  220.     //
  221.     int $degu  ;
  222.     string $inAttr = $lastSrf + ".degreeU" ;
  223.     $degu = `getAttr $inAttr` ;
  224.  
  225.     int $degv  ;
  226.     $inAttr = $lastSrf + ".degreeV" ;
  227.     $degv = `getAttr $inAttr` ;
  228.  
  229.     int $nsu ;
  230.     $inAttr = $lastSrf + ".su" ;
  231.     $nsu = `getAttr $inAttr` ;
  232.     
  233.     int $nsv ;
  234.     $inAttr = $lastSrf + ".sv" ;
  235.     $nsv = `getAttr $inAttr` ;
  236.  
  237.     int $fu ;
  238.     $inAttr = $lastSrf + ".fu" ;
  239.     $fu = `getAttr $inAttr` ;
  240.  
  241.     int $fv ;
  242.     $inAttr = $lastSrf + ".fv" ;
  243.     $fv = `getAttr $inAttr` ;
  244.  
  245.     // 3. Extract the Knots.
  246.     //
  247.     float $knotsU[] ;
  248.     float $knotsV[] ;
  249.     select -r $lastSrf ;
  250.     $knotsU = nurbsSurfaceKnotsAlongU() ;
  251.     $knotsV = nurbsSurfaceKnotsAlongV() ;
  252.     // 4. Scale the Knot Sequence to lie in between [$startParm, $endParm].
  253.     //
  254.     float $sKnotsU[] ;
  255.     $sKnotsU = rescale( $knotsU, $startParm, $endParm ) ;
  256.  
  257.     // 5. Build a temporary surface with the scaled Knots
  258.     //
  259.     int $okay = 1 ;
  260.     string $tmpSurface ;
  261.     $tmpSurface = buildTemporarySurface( $sKnotsU, $degu, $nsu, $fu, $knotsV, $degv, $nsv, $fv ) ;
  262.     if( $tmpSurface == " " ) {
  263.         $okay = 0 ;
  264.     } 
  265.  
  266.     // 6. Rebuild surface inplace to match knots in the U direction.
  267.     //
  268.     if( $okay == 1 )  {
  269.         int $dir = 0 ;
  270.         $okay = rebuildSurfaceToMatchKnots( $lastSrf, $tmpSurface, $dir ) ;
  271.         delete $tmpSurface ;
  272.     }
  273.  
  274.     // 7. select surface for which knots are reparameterized.
  275.     //
  276.     select -r $lastSrf ;
  277.  
  278.     return $okay ;
  279. }
  280.  
  281.